package com.ay.weixin.fdc.boss.user.db;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import lizar.db.ibatis.Dao;

import org.lizar.config.keys.Keys;
import org.lizar.exception.LifecycleException;
import org.lizar.ioc.Lifecycle;

import com.ay.weixin.fdc.boss.user.domain.UserBasic;

public class UserDao extends Dao implements Lifecycle{
	
	public static String user_table_name=null;
	
	public List<UserBasic> getMyInviteListFirstPage(String invite_code) throws SQLException{
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("table",user_table_name);
		map.put("invite_code",invite_code);
		map.put("size",Keys.getInt("usercenter.my_invite_list_first_page_size"));
		return (List<UserBasic>)this.sqlMapper.queryForList("user.getMyInviteListFirstPage",map);
	}
	
	public void imcreate_black_list(long user_id) throws SQLException{
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("table",user_table_name);
		map.put("id",user_id);
		this.sqlMapper.update("user.imcreate_black_list",map);
	}
	
	public void updatePassword(long id,String password) throws SQLException{
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("table",user_table_name);
		map.put("id",id);
		map.put("password",password);
		this.sqlMapper.update("user.updatePassword",map);
	}
	
	public void clear_today_recommand_num(long id) throws SQLException{
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("table",user_table_name);
		map.put("id",id);
		this.sqlMapper.update("user.clear_today_recommand_num",map);
	}
	
	public void updateType(long id,String type) throws SQLException{
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("table",user_table_name);
		map.put("id",id);
		map.put("type",type);
		this.sqlMapper.update("user.updateType",map);
	}
	
	public void resetBlackList(long id) throws SQLException{
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("table",user_table_name);
		map.put("id",id);
		this.sqlMapper.update("user.resetBlackList",map);
	}
	
	public List<UserBasic> getMyInviteListNextPage(String invite_code) throws SQLException{
		Map<String,String> map=new HashMap<String,String>();
		map.put("table",user_table_name);
		map.put("invite_code",invite_code);
		map.put("size",Keys.getStr("usercenter.my_invite_list_first_page_size"));
		return (List<UserBasic>)this.sqlMapper.queryForList("user.getMyInviteListNextPage",map);
	}
	
	
	
	public int getMyInviteNumber(String invite_code) throws SQLException{
		Map<String,String> map=new HashMap<String,String>();
		map.put("table",user_table_name);
		map.put("invite_code",invite_code);
		return (Integer)this.sqlMapper.queryForObject("user.getMyInviteNumber",map);
	}

	@Override
	public void born() throws LifecycleException, Exception {
		user_table_name=Keys.getStr("usercenter.user_table_name");
	}

	@Override
	public void die() throws LifecycleException {
		
	}
	

	public List<UserBasic> getByStatus(long wxproject_id,String status) throws SQLException{
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("table",user_table_name);
		map.put("status",status);
		map.put("wxproject_id",wxproject_id);
		return (List<UserBasic>)this.sqlMapper.queryForList("user.getByStatus",map);
	}
	
	public UserBasic getByInviteCode(String invite_code)throws SQLException{
		Map<String,String> map=new HashMap<String,String>();
		map.put("table",user_table_name);
		map.put("invite_code",invite_code);
		return (UserBasic)this.sqlMapper.queryForObject("user.getByInviteCode",map);
		
	}
	
	public UserBasic getById(long id) throws SQLException{
		Map<String,String> map=new HashMap<String,String>();
		map.put("table",user_table_name);
		map.put("id",Long.toString(id));
		return (UserBasic)this.sqlMapper.queryForObject("user.getById", map);
	}
	
	public void softDelete(long id,String status) throws SQLException{
		Map<String,String> map=new LinkedHashMap<String,String>();
		map.put("status", status);
		map.put("id", id+"");
		map.put("table",user_table_name);
		this.sqlMapper.update("user.softDelete",map);
	}
	
	public void updateRegisterReward(long id) throws SQLException{
		Map<String,Object> map=new LinkedHashMap<String,Object>();
		map.put("register_reward", UserBasic.REGISTER_REWARD_TRUE);
		map.put("id", id);
		map.put("table",user_table_name);
		this.sqlMapper.update("user.updateRegisterReward",map);
	}
	
	public boolean hasRegisterRecord(long id) throws SQLException{
		Map<String,Object> map=new LinkedHashMap<String,Object>();
		map.put("register_reward", UserBasic.REGISTER_REWARD_TRUE);
		map.put("id", id);
		map.put("table",user_table_name);
		return this.sqlMapper.queryForObject("user.hasRegisterRecord",map)!=null;
	}
	
	public void updateStatus(long id,String status,int profile_verify) throws SQLException{
		Map<String,String> map=new LinkedHashMap<String,String>();
		map.put("status", status);
		map.put("id", id+"");
		map.put("profile_verify", profile_verify+"");
		map.put("table",user_table_name);
		this.sqlMapper.update("user.updateStatus",map);
	}
	
	public void delete(long id) throws SQLException{
		Map<String,String> map=new LinkedHashMap<String,String>();
		map.put("id", id+"");
		map.put("table",user_table_name);
		this.sqlMapper.update("user.delete",map);
	}
	
	public void updateInviteCode(long user_id,String invite_code) throws SQLException{
		Map<String,String> map=new LinkedHashMap<String,String>();
		map.put("id", Long.toString(user_id));
		map.put("invite_code", invite_code);
		map.put("table",user_table_name);
		this.sqlMapper.update("user.updateInviteCode",map);
	}
	
	public UserBasic getByPhone(long wxproject_id,String phone)throws SQLException{
		Map<String,Object> map=new LinkedHashMap<String,Object>();
		map.put("phone", phone);
		map.put("wxproject_id", wxproject_id);
		map.put("table",user_table_name);
		return (UserBasic)this.sqlMapper.queryForObject("user.getByPhone",map);
		
	}
	
	public UserBasic getByName(long wxproject_id,String name)throws SQLException{
		Map<String,Object> map=new LinkedHashMap<String,Object>();
		map.put("name", name);
		map.put("wxproject_id", wxproject_id);
		map.put("table",user_table_name);
		return (UserBasic)this.sqlMapper.queryForObject("user.getByName",map);
		
	}
	
	
	public boolean inviteCodeValid(String invite_code) throws SQLException{
		Map<String,String> map=new LinkedHashMap<String,String>();
		map.put("invite_code", invite_code);
		map.put("table",user_table_name);
		Object obj=this.sqlMapper.queryForObject("user.inviteCodeValid",map);
		if(obj==null)return false;
		return true;
	}
	
	public boolean nameExists(long wxproject_id,String name) throws SQLException{
		Map<String,Object> map=new LinkedHashMap<String,Object>();
		map.put("name", name);
		map.put("wxproject_id", wxproject_id);
		map.put("table",user_table_name);
		Object obj=this.sqlMapper.queryForObject("user.nameExists",map);
		if(obj==null)return false;
		return true;
	}
	
	public boolean phoneExists(long wxproject_id,String phone) throws SQLException{
		Map<String,Object> map=new LinkedHashMap<String,Object>();
		map.put("phone", phone);
		map.put("wxproject_id", wxproject_id);
		map.put("table",user_table_name);
		Object obj=this.sqlMapper.queryForObject("user.phoneExists",map);
		if(obj==null)return false;
		return true;
	}
}
